package guo.mybatis;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserService {
    private static final String SQL_SELECT = "select name from t_user where id=?";

    public String selectNameById(int id) {
        String name = null;
        try (UserDao UserDao = new UserDao(SQL_SELECT)) {
            PreparedStatement statement = UserDao.getStatement();
            statement.setInt(1,id);
            ResultSet resultSet =  statement.executeQuery();
            if (resultSet.next()){
                name = resultSet.getString("name");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return name;
    }
}
